import {
  ComposableMenu,
  ConditionalItem,
  designTreeMenuManager,
  featureFilteredArray,
  IContextMenu,
  useCommandManager,
} from '@amcax/base';
import { DocumentContext } from '@app-cad/common/providers/documentProvider';
import { useContext } from 'react';
import { ProductTreeNode } from '../types';
import {
  editAssemblyConstraint,
  exchange,
  hide,
  invert,
  open,
  removeAssemblyChild,
  removeAssemblyConstraint,
  removeTranslucency,
  rename,
  resort,
  show,
  translucency,
} from './menuItems';
import { removeMeasurement } from './menuItems/removeMeasurement';
import { viewAssemblyConstraints } from '@app-cad-env/product/widgets/LeftPanel/ContextMenu/menuItems/viewAssemblyConstraints';
import { removeReference } from './menuItems/removeReference';

const PMenu = ({ id }: { id: string }) => {
  const { docTree } = useContext(DocumentContext);
  const { invokeCmd } = useCommandManager();

  return (
    <ComposableMenu<ProductTreeNode>
      id={id}
      menuItems={featureFilteredArray([
        // menu items for assembly child
        open,
        show,
        hide,
        new ConditionalItem('set-opacity', translucency),
        new ConditionalItem('set-opacity', removeTranslucency),
        rename,
        removeAssemblyChild,
        removeMeasurement,
        removeReference,
        resort,
        exchange,
        viewAssemblyConstraints,
        // menu items for assembly constraint
        editAssemblyConstraint,
        removeAssemblyConstraint,
        invert,
      ])}
      environmentData={{ docTree, invokeCmd }}
    />
  );
};

export const ProductNodeMenu: IContextMenu = {
  id: 'productNodeMenu',
  render: (id: string) => (
    <PMenu
      id={id}
      key={`product_context_menu_${id}`}
    />
  ),
};

designTreeMenuManager.register(ProductNodeMenu);
